//
//  RYBiaoqianChoiceView.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/19.
//  Copyright © 2021 叁拾叁. All rights reserved.
//


import UIKit

/// 列表页横向标签选择控件
class RYBiaoqianChoiceView: UIView {
    
    public var clickedClosure: ((Int) -> Void)?
    
    public var items: [RYListSelectItem] = []
    
    public var itemWidth: CGFloat = 0.0
    
    public var lastIndex: Int = 0
    
    public var bgView: UIScrollView = {
        let v:UIScrollView = UIScrollView()
        
        return v
    }()
    
    public var signView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = UIColor.white
        v.layer.cornerRadius = 1
        v.backgroundColor = UIColor(hex: 0x367FF6)
        return v
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    public func configureView(titles: [String]? = nil) {
        
        if let t = titles {
            if items.count == 0 {
                initViews(t.count)
            }
            for (i, value) in items.enumerated() {
                value.titleLb.text = t[i]
            }
        }
    }
    
    public func refreshItems(_ index: Int) {
        
        if lastIndex == index {
            return
        }
        lastIndex = index
        for (i, value) in items.enumerated() {
            if i == index {
                value.titleLb.textColor = UIColor(hex: 0x367FF6)
                let x = self.itemWidth * CGFloat(index) + (self.itemWidth - 55) / 2
                if x < self.bgView.contentOffset.x || x+55 > self.bgView.contentOffset.x + self.frame.size.width {
                    let OffSetX = (x + self.itemWidth - self.frame.size.width)
                    if OffSetX > 0 {
                        self.bgView.setContentOffset(CGPoint(x: OffSetX, y: 0), animated: true)
                    } else {
                        self.bgView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
                    }
                }
                UIView.animate(withDuration: 0.25) {
                    self.signView.frame = CGRect(x: x, y: self.frame.size.height - 2, width: 55, height: 2)
                }
            } else {
                value.titleLb.textColor = UIColor(hex: 0x333333)
            }
        }
    }
    
    fileprivate func initViews(_ count: Int) {
        
        itemWidth = self.frame.size.width / CGFloat(5)
        bgView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
        bgView.contentSize = CGSize(width: itemWidth*CGFloat(count), height: self.frame.size.height)
        bgView.showsVerticalScrollIndicator = false
        bgView.showsHorizontalScrollIndicator = false
        self.addSubview(bgView)
        for i in 0..<count {
            let item: RYListSelectItem = RYListSelectItem(frame: CGRect(x: itemWidth * CGFloat(i), y: 0, width: itemWidth, height: self.frame.size.height))
            item.tag = 100 + i
            item.clickedClosure = {[weak self] index in
                if let weakSelf = self {
                    weakSelf.clickedClosure?(index)
                    weakSelf.refreshItems(index - 100)
                }
            }
            items.append(item)
            bgView.addSubview(item)
        }
        signView.frame = CGRect(x: (itemWidth - 55) / 2, y: self.frame.size.height - 3, width: 55, height: 2)
        bgView.addSubview(signView)
    }
}


/// 列表页选择控件item
class RYListSelectItem: UIView {
    
    public var clickedClosure: ((Int) -> Void)?
    
    public var titleLb: UILabel = {
        let v: UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.font = UIFont.systemFont(ofSize: 13)
        return v
    }()
    
    public lazy var aboveBtn: UIButton = {
        let v: UIButton = UIButton()
        v.addTarget(self, action: #selector(aboveBtnAction), for: .touchUpInside)
        return v
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    @objc func aboveBtnAction() {
        clickedClosure?(self.tag)
    }
    
    fileprivate func initViews() {
        
        addSubview(titleLb)
        addSubview(self.aboveBtn)
        
        titleLb.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.height.equalTo(18)
        }
        
        aboveBtn.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}
